package com.lry.spi;

import java.util.Comparator;

import static java.lang.Integer.compare;

/**
 * @author JiangJie
 * @create 2020-12-22 15:23
 */
public interface Prioritized extends Comparable<Prioritized> {
    Comparator<Object> COMPARATOR = (one, two) -> {
        boolean b1 = one instanceof Prioritized;
        boolean b2 = two instanceof Prioritized;
        if (b1 && !b2) {        // one is Prioritized, two is not
            return -1;
        } else if (b2 && !b1) { // two is Prioritized, one is not
            return 1;
        } else if (b1 && b2) {  //  one and two both are Prioritized
            return ((Prioritized) one).compareTo((Prioritized) two);
        } else {                // no different
            return 0;
        }
    };

    int MAX_PRIORITY = Integer.MIN_VALUE;

    int MIN_PRIORITY = Integer.MAX_VALUE;

    int NORMAL_PRIORITY = 0;

    default int getPriority() {
        return NORMAL_PRIORITY;
    }

    @Override
    default int compareTo(Prioritized that) {
        return compare(getPriority(), that.getPriority());
    }

}
